package com.hanserwei.community.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hanserwei.community.entity.common.ApiResponse;
import com.hanserwei.community.entity.po.Building;
import com.hanserwei.community.entity.request.building.BuildingAddRequest;
import com.hanserwei.community.entity.request.building.BuildingQueryRequest;
import com.hanserwei.community.service.BuildingService;
import com.hanserwei.community.uitils.ThrowUtils;
import com.hanserwei.community.enums.ResponseCodeEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/building")
@RequiredArgsConstructor
public class BuildingController {

    private final BuildingService buildingService;

    @PostMapping("/search")
    @Operation(summary = "条件查询建筑列表")
    public ApiResponse<Page<Building>> queryBuildingByPage(@RequestBody BuildingQueryRequest request) {
        Page<Building> result = buildingService.queryBuildingByPage(request);
        return ApiResponse.success("查询成功！", result);
    }

    @GetMapping("/list")
    @Operation(summary = "查询建筑列表")
    public ApiResponse<Page<Building>> listBuilding(
            @Parameter(description = "当前页码，例如：1") @RequestParam("currentNumber") Integer currentNumber,
            @Parameter(description = "每页显示数量，例如：10") @RequestParam("pageSize") Integer pageSize
    ) {
        Page<Building> result = buildingService.listByPage(currentNumber, pageSize);
        return ApiResponse.success("查询成功！", result);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除建筑")
    public ApiResponse<Boolean> deleteBuilding(@Parameter(description = "建筑ID")
                                               @RequestParam("id") Integer id) {
        boolean result = buildingService.removeById(id);
        ThrowUtils.throwIf(!result, ResponseCodeEnum.SYSTEM_ERROR, "删除失败！");
        return ApiResponse.success("删除成功！", true);
    }

    @PostMapping("/add")
    @Operation(summary = "添加建筑")
    public ApiResponse<Boolean> addBuilding(@RequestBody BuildingAddRequest request) {
        ThrowUtils.throwIf(request == null, ResponseCodeEnum.INVALID_PARAMETER, "参数错误！");
        boolean result = buildingService.addBuilding(request);
        return ApiResponse.success("添加成功！", result);
    }

    @PostMapping("/update/{id}")
    @Operation(summary = "更新建筑")
    public ApiResponse<Boolean> updateBuilding(@RequestBody BuildingAddRequest request,
                                               @PathVariable Integer id) {
        ThrowUtils.throwIf(request == null, ResponseCodeEnum.INVALID_PARAMETER, "参数错误！");
        boolean result = buildingService.updateBuilding(request, id);
        return ApiResponse.success("更新成功！", result);
    }

    @DeleteMapping("/deleteBatch")
    @Operation(summary = "批量删除建筑")
    public ApiResponse<Boolean> deleteBuildingBatch(@Parameter(description = "建筑ID列表") @RequestBody Integer[] ids) {
        ThrowUtils.throwIf(ids == null || ids.length == 0, ResponseCodeEnum.INVALID_PARAMETER, "参数错误！");
        boolean result = buildingService.removeBatchByIds(List.of(ids));
        ThrowUtils.throwIf(!result, ResponseCodeEnum.SYSTEM_ERROR, "删除失败！");
        return ApiResponse.success("删除成功！", true);
    }

    @GetMapping("/all")
    @Operation(summary = "获取所有建筑列表（用于下拉选择）")
    public ApiResponse<List<Building>> getAllBuildings() {
        List<Building> result = buildingService.list();
        return ApiResponse.success("查询成功！", result);
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据ID查询建筑详情")
    public ApiResponse<Building> getBuildingById(@Parameter(description = "建筑ID") @PathVariable Integer id) {
        ThrowUtils.throwIf(id == null || id <= 0, ResponseCodeEnum.INVALID_PARAMETER, "建筑ID参数错误！");
        Building result = buildingService.getById(id);
        ThrowUtils.throwIf(result == null, ResponseCodeEnum.RESOURCE_NOT_FOUND, "建筑不存在！");
        return ApiResponse.success("查询成功！", result);
    }

}
